module.exports = function(app){

    var express = require('express')
    ,   router  = express.Router()
    ,   routes  = app.get('routes')
<<<<<<< HEAD
    ,   core    = require('core')
=======
    ,   core    = require(uri)
>>>>>>> 1be9a16daf7636bc34bee9c37e87bd23f93d4fd2
    ,   api     = require(routes+'/api/index')()
    ,   db      = core.db
    ,   auth    = core.auth
    ,   error   = core.error
    ,   success = core.success;

    var API = function(req, res, next) {

        if(!db.status)
            return res.send(function(){
                var err = error(3);
                err.data = [];
                return err;
            }());
        var platform = api[req.params.platform];
        var rdata = error(404);
        rdata.data = [];

        if(platform){
            var Class   = req.params.class != 'index' ? function(){

                var Class = req.params.class.split('_')

                if(Class.length == 1)
                    platform = platform[Class[0]];
                else{
                    for(var i in Class){
                        platform = platform[Class[i]];
                        if(!platform)
                            break;
                    };
                }
                if(platform)
                    return platform;
                else
                    return false;

            }() : api.auth
<<<<<<< HEAD
            ,   Auth    = typeof Class !== 'function' ? auth({sign : Class.sign || false, auth: Class.auth || false}, (JSON.stringify(req.body) !== "{}" ? req.body : req.query)) : function(){
                return auth({sign : Class.sign || false, auth: Class.auth || false}, JSON.stringify(req.body) !== "{}" ? req.body : req.query)
=======
            ,   Auth    = typeof Class !== 'function' ? auth({sign : Class.sign || false, auth: Class.auth || false}, req.params.platform, req.query) : function(){
                return auth({sign : Class.sign || false, auth: Class.auth || false}, req.params.platform, req.query)
>>>>>>> 1be9a16daf7636bc34bee9c37e87bd23f93d4fd2
            };

            if(Class){
                req.example = {
                    success : success,
                    error   : error
                };
                return function(){
                    if(typeof Class === 'function')
                        Class(req.app, function(){
                            Auth = Auth();
<<<<<<< HEAD
                            if(typeof Auth !== 'function')
                                return function(){
                                    rdata.code = Auth.code;
                                    rdata.msg  = Auth.msg;
                                    return res.send(rdata);
                                }();
                            Auth((function(auth){
                                if(auth.code){
                                    rdata.code = auth.code;
                                    rdata.msg  = auth.msg;
                                    return res.send(rdata);
                                };

                                Class.callback(req, res, next);
                            }));
                        });
                    else{
                        if(typeof Auth !== 'function')
                            return function(){
                                rdata.code = Auth.code;
                                rdata.msg  = Auth.msg;
                                return res.send(rdata);
                            }();
                        Auth(function(auth){

                            if(auth.code){
                                rdata.code = auth.code;
                                rdata.msg  = auth.msg;
                                return res.send(rdata);
                            };
                            Class.callback(req, res, next);
                        });

=======
                            if(Auth.code){
                                rdata.code = Auth.code;
                                rdata.msg  = Auth.msg;
                                return res.send(rdata);
                            };

                            Class.callback(req, res, next);
                        });
                    else{
                        if(Auth.code){
                            rdata.code = Auth.code;
                            rdata.msg  = Auth.msg;
                            return res.send(rdata);
                        };
                        Class.callback(req, res, next);
>>>>>>> 1be9a16daf7636bc34bee9c37e87bd23f93d4fd2
                    };

                }();
            }else if(!Class)
                return res.send(rdata);
            else{
                rdata.code = Auth.code;
                rdata.msg  = Auth.msg;
                return res.send(rdata);
            };

        }else return res.send(rdata);


    };

    /* GET home page. */
    router.post('/:platform/:class', API);
    router.post('/:platform', function(req, res, next){
        req.params.class = 'index';
        API(req, res, next);
    });

    return router;
};
